package com.zzzy.cont.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzzy.cont.dao.entity.ConUserOperateLink;
import com.zzzy.cont.dao.mapper.ConUserOperateLinkMapper;
import com.zzzy.framework.common.util.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 合同用户操作关联表 服务实现类
 *
 * @author fc
 * @since 2024-06-12
 */
@Service
public class ConUserOperateLinkService extends ServiceImpl<ConUserOperateLinkMapper, ConUserOperateLink> {

    public void addByConId(Long conId) {
        if (!isLoginUserLinkByConId(conId)){
            ConUserOperateLink conUserOperateLink = new ConUserOperateLink();
            conUserOperateLink.setConId(conId);
            conUserOperateLink.setUserId(SecurityUtils.getLoginUser().getId());
            baseMapper.insert(conUserOperateLink);
        }
    }

    /**
     * 当前用户是否是合同经办人
     */
    public boolean isLoginUserLinkByConId(Long conId) {
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<ConUserOperateLink>().eq(ConUserOperateLink::getConId, conId)
                .eq(ConUserOperateLink::getUserId, SecurityUtils.getLoginUser().getId()));
        return count > 0;
    }

    public boolean isContainsLink(Long conId, Set<Long> userIdSet) {
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<ConUserOperateLink>().eq(ConUserOperateLink::getConId, conId)
                .in(ConUserOperateLink::getUserId, userIdSet));
        return count > 0;
    }

    public List<Long> getOperateLinkUserIdListByConId(Long conId) {
        List<ConUserOperateLink> operateLinkList = baseMapper.selectList(new LambdaQueryWrapper<ConUserOperateLink>().eq(ConUserOperateLink::getConId, conId));
        return operateLinkList.stream().map(ConUserOperateLink::getUserId).collect(Collectors.toList());
    }
}
